package com.waya.web.browser.util;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.ValueCallback;
import java.util.List;
import org.jetbrains.annotations.NotNull;

/**
 * Created by JesseHev on 2017/12/4.
 * describe web cookie 管理工具
 */

public class WebCookieUtil {

  private static final String TAG = "WebCookieUtil";

  public static final boolean LOLLIPOP = Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP;

  public WebCookieUtil(Context context) {
    createCookiesSyncInstance(context);


  }

  private void createCookiesSyncInstance(Context context) {
    if (LOLLIPOP) {
      CookieSyncManager.createInstance(context);
    }
  }

  /**
   * 获取cookie
   *
   * @param url 请求地址
   */
  public String getCookie(@NotNull String url) {
    CookieManager cookieManager = CookieManager.getInstance();
    if (cookieManager == null) {
      throw new NullPointerException("failed to get cookie instance!");
    }
    return cookieManager.getCookie(url);
  }

  /**
   * 同步cookie
   *
   * @param url 请求地址
   * @param cookie 需要同步的cookie
   */
  public void syncCookie(@NotNull String url, @NotNull String cookie) {
    CookieManager cookieManager = CookieManager.getInstance();
    if (cookieManager != null) {
      cookieManager.setCookie(url, cookie);
      flush();
    }
  }

  /**
   * 同步多个cookie
   *
   * @param url 请求地址
   * @param cookies cookies
   */
  public void syncCookies(@NotNull String url, @NotNull List<String> cookies) {
    CookieManager cookieManager = CookieManager.getInstance();
    removeAllCookies(null);
    cookieManager.setAcceptCookie(true);
    for (String cookie : cookies) {
      cookieManager.setCookie(url, cookie);
    }
    flush();
  }

  /**
   * 移除cookie
   *
   * @param callback 回调
   */
  public void removeAllCookies(@Nullable ValueCallback<Boolean> callback) {
    if (callback == null) {
      callback = getDefaultCookieCallback();
    }
    if (CookieManager.getInstance() == null) {
      callback.onReceiveValue(new Boolean(false));
      return;
    }
    if (LOLLIPOP) {
      CookieManager.getInstance().removeSessionCookie();
      callback.onReceiveValue(new Boolean(true));
      flush();
      return;
    }
    CookieManager.getInstance().removeSessionCookies(callback);
    flush();
  }

  /**
   * 移除过期的cookie
   */
  public void removeExpirdCookie() {
    CookieManager cookieManager = CookieManager.getInstance();
    if (cookieManager == null) {
      return;
    }
    cookieManager.removeExpiredCookie();
    flush();
  }

  /**
   * 同步cookie
   */
  private void flush() {
    if (LOLLIPOP) {
      CookieSyncManager.getInstance().sync();
    } else {
      AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
        @Override public void run() {
          CookieManager.getInstance().flush();
        }
      });
    }
  }

  private static ValueCallback<Boolean> getDefaultCookieCallback() {

    return new ValueCallback<Boolean>() {
      @Override public void onReceiveValue(Boolean ignore) {
        Log.d(TAG, "removeExpiredCookies:" + ignore);
      }
    };
  }
}
